package com.oscar.ehome;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.base.C;
import com.oscar.ehome.bean.WeclomeBean;
import com.oscar.ehome.bean.loginbean.UserBean;
import com.oscar.ehome.bean.pagebean.DiaoChaInfo;
import com.oscar.ehome.bean.pagebean.StudyDetialBean;
import com.oscar.ehome.presenter.WeclomePresenter;
import com.oscar.ehome.provider.gildeimage.GildeUtils;

import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.VersionUtil;
import com.oscar.ehome.utils.view.progressview.CircleTextProgressbar;
import com.oscar.ehome.view.konwledge.KnowledgeActivity;
import com.oscar.ehome.view.loginview.ApproveActivity;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.BannerWebActivity;
import com.oscar.ehome.view.pageview.FrequencyActivity;
import com.oscar.ehome.view.pageview.PdfDetialActivity;
import com.oscar.ehome.view.pageview.VideoActivity;
import com.oscar.ehome.view.welcomeview.LeadActivity;
import com.oscar.ehome.view.welcomeview.WeclomeView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WelcomeActivity extends Activity implements OnClickListener, WeclomeView {

    /**
     * 显示广告图片
     */
    ImageView img;
    /**
     * 跳过
     */
    CircleTextProgressbar tv_red_skip;
    /**
     * 时间
     */
    TextView sec_text;

    private boolean adIsFinish = false;
    private int delayTime = 3;// 3秒
    boolean login;
    String token = "", tiaozhuan = "";
    private DiaoChaInfo diaoChaInfo;
    private StudyDetialBean wenjian;
    private WeclomePresenter weclomePresenter;
    UserBean userbean;
    private final int UPDATE_TEAY_TIME = 103;

    private   Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case UPDATE_TEAY_TIME:// 无操作三秒后跳转
                    if (adIsFinish) {
                        delayTime--;
                        sec_text.setText(delayTime + "s");
                        if (delayTime > 0) {
                            handler.sendEmptyMessageDelayed(UPDATE_TEAY_TIME, 1000);
                        } else {
                            intent();
                        }
                    } else {
                        intent();
                    }
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        byId();
        onClicks();
        initView();
    }

    /**
     * ID
     */
    public void byId() {
        img =  findViewById(R.id.img);
        sec_text = findViewById(R.id.sec_text);
        tv_red_skip =findViewById(R.id.tv_red_skip);

    }

    /**
     * 点击事件
     */
    public void onClicks() {
        img.setOnClickListener(this);
        tv_red_skip.setOnClickListener(this);
    }

    /**
     * 加载数据
     */
    public void initView() {
        weclomePresenter = new WeclomePresenter(this);

        tv_red_skip.setVisibility(View.GONE);
        sec_text.setVisibility(View.GONE);

        login = SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getLogin();
        if (login) {//已登陆
            token = SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getToken();
            String user = SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getUserBean();
            userbean = new Gson().fromJson(user, UserBean.class);
            weclomePresenter.getImg(token);

        } else {
            handler.sendEmptyMessageDelayed(UPDATE_TEAY_TIME, 1000);

        }
        CommonUtils.context = getApplicationContext();
        //startThread();
    }

    /**
     * 跳转页面
     */
    public void intent() {

        Intent intent;
        CommonUtils.versioncode = VersionUtil.getVersionName(WelcomeActivity.this);
        if (SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getIsFirst()) {//不是第一次安装
            if (SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getLogin()) {
                if (userbean != null) {
                    if (SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getStatus().equals("1")) {
                        if (userbean.getIsPay() != null && "1".equals(userbean.getIsPay())) {
                            if (diaoChaInfo != null && !diaoChaInfo.equals("")) {
                                intent = new Intent(WelcomeActivity.this, DiaoChaActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                WelcomeActivity.this.startActivity(intent);
                                WelcomeActivity.this.overridePendingTransition(R.anim.activity_open, R.anim.jingzi);
                                WelcomeActivity.this.finish();
                            } else {
                                if (wenjian != null && !wenjian.equals("")) {
                                    String learnType = wenjian.getLearnType();
                                    if ("video/mp4".equals(learnType)) {//视频
                                        intent = new Intent(WelcomeActivity.this, VideoActivity.class);
                                        intent.putExtra("id", wenjian.getId());
                                        intent.putExtra("type", "qiangzhi");
                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        WelcomeActivity.this.startActivity(intent);
                                        WelcomeActivity.this.overridePendingTransition(R.anim.activity_open, R.anim.jingzi);
                                        WelcomeActivity.this.finish();
                                    } else if ("application/pdf".equals(learnType)) {//PDF
                                        intent = new Intent(WelcomeActivity.this, PdfDetialActivity.class);
                                        intent.putExtra("id", wenjian.getId());
                                        intent.putExtra("type", "qiangzhi");
                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        WelcomeActivity.this.startActivity(intent);
                                        WelcomeActivity.this.overridePendingTransition(R.anim.activity_open, R.anim.jingzi);
                                        WelcomeActivity.this.finish();
                                    } else if ("audio/mp3".equals(learnType)) {//音频
                                        intent = new Intent(WelcomeActivity.this, FrequencyActivity.class);
                                        intent.putExtra("id", wenjian.getId());
                                        intent.putExtra("type", "qiangzhi");
                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        WelcomeActivity.this.startActivity(intent);
                                        WelcomeActivity.this.overridePendingTransition(R.anim.activity_open, R.anim.jingzi);
                                        WelcomeActivity.this.finish();
                                    }

                                } else {
                                    if ("1".equals(SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getManfer())) {//内勤管理员
                                        intent = new Intent(WelcomeActivity.this, MainActivity.class);//MainActivity
                                        intent.putExtra("intent", 0);
                                    } else if ("2".equals(SystemConfigFactory.getInstance(WelcomeActivity.this).getSystemConfig().getManfer())) {//律师团队管理
                                        intent = new Intent(WelcomeActivity.this, KnowledgeActivity.class);
                                        intent.putExtra("intent", 0);
                                    } else {

                                        intent = new Intent(WelcomeActivity.this, MainActivity.class);//MainActivity
                                        intent.putExtra("intent", 0);

                                    }
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    WelcomeActivity.this.startActivity(intent);
                                    WelcomeActivity.this.overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
                                    WelcomeActivity.this.finish();
                                }
                            }
                        } else {

                            intent = new Intent(WelcomeActivity.this, MainActivity.class);//MainActivity
                            intent.putExtra("intent", 0);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            WelcomeActivity.this.startActivity(intent);
                            WelcomeActivity.this.overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
                            WelcomeActivity.this.finish();
                        }
                    } else {
                        intent = new Intent(WelcomeActivity.this, ApproveActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        WelcomeActivity.this.startActivity(intent);
                        WelcomeActivity.this.overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
                        WelcomeActivity.this.finish();
                    }

                }


            } else {
                intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                WelcomeActivity.this.startActivity(intent);
                WelcomeActivity.this.overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
                WelcomeActivity.this.finish();
            }
        } else {
            intent = new Intent(WelcomeActivity.this, LeadActivity.class);
            intent.putExtra("lead", 1);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            WelcomeActivity.this.startActivity(intent);
            WelcomeActivity.this.overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
            WelcomeActivity.this.finish();
        }


    }

    /**
     * 开启3秒延时
     */


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.img:// 广告图片点击事件
                if (!tiaozhuan.equals("")) {
                    stop_hadler();
                    Intent intent = new Intent(WelcomeActivity.this, BannerWebActivity.class);
                    intent.putExtra("contentsrc", tiaozhuan);
                    intent.putExtra("type", "banner");
                    intent.putExtra("name", "");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.tv_red_skip:// 跳过
                intent();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        stop_hadler();
        super.onDestroy();
    }

    public void stop_hadler() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

    @Override
    public void moveToIndex(WeclomeBean resule) {

        List<WeclomeBean.AppListBean> listBean = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        listBean = resule.getList();

        for (int i = 0; i < listBean.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("picture", C.qiniu_ip + listBean.get(i).getPicture());
            map.put("showtime", listBean.get(i).getShowBeginTime());
            map.put("endtime", listBean.get(i).getShowEndTime());

            list.add(map);


        }

        if (list.size() > 0) {
            tiaozhuan = listBean.get(0).getUrl();
            GildeUtils.loadUrlImages(C.qiniu_ip + listBean.get(0).getPicture(), img, WelcomeActivity.this);
            tv_red_skip.setVisibility(View.VISIBLE);
            sec_text.setVisibility(View.VISIBLE);
            adIsFinish = true;
            //设置进度3秒
            tv_red_skip.setTimeMillis(3000);
            //开启进度
            handler.sendEmptyMessageDelayed(UPDATE_TEAY_TIME, 1000);
            //开启进度展示
            tv_red_skip.start();
        }


        weclomePresenter.getwelDiaochaInfo(token);
        weclomePresenter.getwenjianInfo(token);
    }

    @Override
    public void getDiaocharesult(DiaoChaInfo result) {
        diaoChaInfo = result;

    }

    @Override
    public void getbaojianjufile(StudyDetialBean result) {
        wenjian = result;
    }

    @Override
    public void showToast(String msg) {
        try {
            handler.sendEmptyMessageDelayed(UPDATE_TEAY_TIME, 1000);
        }catch (Exception e){
            intent();
        }


    }
}
